﻿Imports System.Drawing

''' <summary>
''' Represents strongly typed settings contained in a PowerSong database.
''' </summary>
Public Class Settings

    Friend FDatabase As Database = Nothing
    Friend FData As Data.SettingsDataTable = Nothing

    Friend Sub New(ByVal database As Database)
        FDatabase = database
        FData = database.Data.Settings
    End Sub

    ''' <summary>
    ''' Gets the value of the given setting.
    ''' </summary>
    ''' <param name="name">The name of the setting whose value to retrieve.</param>
    ''' <returns>The value of the setting whose name was provided.</returns>
    Public Function GetSetting(ByVal name As String) As String
        Return FData.FindByName(name).Value
    End Function

    ''' <summary>
    ''' Changes the value of the given setting.
    ''' </summary>
    ''' <param name="name">The name of the setting whose value to change.</param>
    ''' <param name="value">The new value of the setting identified by the given name.</param>
    Public Sub ChangeSetting(ByVal name As String, ByVal value As String)

        Dim ExistingSetting As Data.SettingsRow = FData.FindByName(name)
        If ExistingSetting IsNot Nothing Then
            ExistingSetting.Value = value
        Else
            FData.AddSettingsRow(name, value)
        End If
        FDatabase.Save()

    End Sub

    ''' <summary>
    ''' Adds a new setting with the given name and value.
    ''' </summary>
    ''' <param name="name">The name of the setting to add.</param>
    ''' <param name="value">The value of the setting to add.</param>
    Public Sub AddSetting(ByVal name As String, ByVal value As String)
        FData.AddSettingsRow(name, value)
        FDatabase.Save()
    End Sub

    ''' <summary>
    ''' Deletes the given setting.
    ''' </summary>
    ''' <param name="name">The name of the setting to remove.</param>
    Public Sub DeleteSetting(ByVal name As String)
        FData.RemoveSettingsRow(FData.FindByName(name))
    End Sub

    ''' <summary>
    ''' Determine whether or not the given setting is defined.
    ''' </summary>
    ''' <param name="name">The name of the setting to search for.</param>
    ''' <returns>True, if the setting exists. False otherwise.</returns>
    Public Function SettingExists(ByVal name As String) As Boolean
        For Each Setting As Data.SettingsRow In FData
            If Setting.Name = name Then Return True
        Next
        Return False
    End Function

    ''' <summary>
    ''' Gets or sets the active bible translation.
    ''' </summary>
    ''' <value>The active bible translation.</value>
    Public Property ActiveBible() As String
        Get
            Return GetSetting("ActiveBible")
        End Get
        Set(ByVal value As String)
            ChangeSetting("ActiveBible", value)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the database version.
    ''' It is not recommended that this value ever be modified.
    ''' </summary>
    ''' <value>The version of the database.</value>
    Public Property DatabaseVersion() As String
        Get
            Return GetSetting("DatabaseVersion")
        End Get
        Set(ByVal value As String)
            ChangeSetting("DatabaseVersion", value)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether song previews should be shown.
    ''' </summary>
    ''' <value><c>true</c> if song previews should be shown; otherwise, <c>false</c>.</value>
    Public Property ShowSongPreviews() As Boolean
        Get
            Return Boolean.Parse(GetSetting("ShowSongPreviews"))
        End Get
        Set(ByVal value As Boolean)
            ChangeSetting("ShowSongPreviews", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether a preview projection should be shown.
    ''' </summary>
    ''' <value>
    ''' <c>true</c> if a preview projection should be shown; otherwise, <c>false</c>.
    ''' </value>
    Public Property ShowPreviewProjection() As Boolean
        Get
            Return Boolean.Parse(GetSetting("ShowPreviewProjection"))
        End Get
        Set(ByVal value As Boolean)
            ChangeSetting("ShowPreviewProjection", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether the main projection should be shown.
    ''' </summary>
    ''' <value><c>true</c> if the main projection should be shown; otherwise, <c>false</c>.</value>
    Public Property ShowMainProjection() As Boolean
        Get
            Return Boolean.Parse(GetSetting("ShowMainProjection"))
        End Get
        Set(ByVal value As Boolean)
            ChangeSetting("ShowMainProjection", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the name of the notice font.
    ''' </summary>
    ''' <value>The name of the notice font.</value>
    Public Property NoticeFontName() As String
        Get
            Return GetSetting("NoticeFontName")
        End Get
        Set(ByVal value As String)
            ChangeSetting("NoticeFontName", value)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the size of the notice font.
    ''' </summary>
    ''' <value>The size of the notice font.</value>
    Public Property NoticeFontSize() As Double
        Get
            Return CDbl(GetSetting("NoticeFontSize"))
        End Get
        Set(ByVal value As Double)
            ChangeSetting("NoticeFontSize", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the default notice font colour.
    ''' </summary>
    ''' <value>The default notice font colour.</value>
    Public Property NoticeDefaultFontColour() As Color
        Get
            Return Color.FromArgb(CInt(GetSetting("NoticeDefaultFontColour")))
        End Get
        Set(ByVal value As Color)
            ChangeSetting("NoticeDefaultFontColour", value.ToArgb.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the default notice background colour.
    ''' </summary>
    ''' <value>The default notice background colour.</value>
    Public Property NoticeDefaultBackgroundColour() As Color
        Get
            Return Color.FromArgb(CInt(GetSetting("NoticeDefaultBackgroundColour")))
        End Get
        Set(ByVal value As Color)
            ChangeSetting("NoticeDefaultBackgroundColour", value.ToArgb.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the notice font style.
    ''' </summary>
    ''' <value>The notice font style.</value>
    Public Property NoticeFontStyle() As FontStyle
        Get
            Return CInt(GetSetting("NoticeFontStyle"))
        End Get
        Set(ByVal value As FontStyle)
            ChangeSetting("NoticeFontStyle", CInt(value).ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the speed of a notice.
    ''' </summary>
    ''' <value>The notice speed.</value>
    Public Property NoticeSpeed() As Double
        Get
            Return CDbl(GetSetting("NoticeSpeed"))
        End Get
        Set(ByVal value As Double)
            ChangeSetting("NoticeSpeed", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the time taken to fade in a notice.
    ''' </summary>
    ''' <value>The time taken for a notice to fade in, in milliseconds.</value>
    Public Property NoticeTimeIn() As Integer
        Get
            Return CInt(GetSetting("NoticeTimeIn"))
        End Get
        Set(ByVal value As Integer)
            ChangeSetting("NoticeTimeIn", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the time taken to fade out a notice.
    ''' </summary>
    ''' <value>The time taken for a notice to fade out, in milliseconds.</value>
    Public Property NoticeTimeOut() As Integer
        Get
            Return CInt(GetSetting("NoticeTimeOut"))
        End Get
        Set(ByVal value As Integer)
            ChangeSetting("NoticeTimeOut", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the default duration of the notice.
    ''' </summary>
    ''' <value>The default duration of the notice.</value>
    Public Property NoticeDefaultDuration() As Integer
        Get
            Return CInt(GetSetting("NoticeDefaultDuration"))
        End Get
        Set(ByVal value As Integer)
            ChangeSetting("NoticeDefaultDuration", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value indicating whether activity logging is enabled.
    ''' </summary>
    ''' <value><c>true</c> if activity logging is enabled; otherwise, <c>false</c>.</value>
    Public Property EnableLogging() As Boolean
        Get
            Return Boolean.Parse(GetSetting("EnableLogging"))
        End Get
        Set(ByVal value As Boolean)
            ChangeSetting("EnableLogging", value.ToString)
        End Set
    End Property

    ''' <summary>
    ''' Sets the default settings.
    ''' </summary>
    Public Sub SetDefaultSettings()

        ActiveBible = "KJV"
        DatabaseVersion = Database.DATABASE_FILE_VERSION
        ShowSongPreviews = True
        ShowMainProjection = True
        ShowPreviewProjection = True
        NoticeFontName = "Arial"
        NoticeFontSize = 55
        NoticeDefaultFontColour = Color.LightCyan
        NoticeDefaultBackgroundColour = Color.DarkSlateBlue
        NoticeFontStyle = FontStyle.Bold
        NoticeSpeed = 0.02
        NoticeTimeIn = 1000
        NoticeTimeOut = 1000
        NoticeDefaultDuration = 30
        EnableLogging = False

    End Sub

End Class
